﻿/****************************** Module Header ******************************\

Copyright (c) Gary Davidson

This source is subject to the Microsoft Public License.
See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
All other rights reserved.

THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;

namespace Savviety.ServiceManager.API.Criteria
{
    public enum UnaryOperator
    {
        IsNull,
        IsNotNull,
    }


    public partial class CriteriaBuilder
    {
       
        protected XNamespace NameSpace = XNamespace.Get("http://Microsoft.EnterpriseManagement.Core.Criteria/");
        protected readonly List<string> Managagementpacks = new List<string>();
        protected readonly List<string> Aliases = new List<string>();
        protected XElement Root;

        public CriteriaBuilder(string nameSpace)
        {
            NameSpace = XNamespace.Get(nameSpace);
            Root = new XElement(NameSpace + "Criteria");
        }
        public CriteriaBuilder(Expression expression)
        {
          
            Root = new XElement(NameSpace + "Criteria");
            var el = Expression.GetXElement("Expression");
            el.Add(expression.ToXElement());
            Root.Add(el);

         
            
        }

        public void AddReference(XElement reference)
        {

            Root.AddFirst(reference);
         

        }

       

        public void AddNotEqualsCriteria(string property, string value)
        {
            var simpleExpression = new SimpleExpression( property, value, ExpressionOperator.NotEqual);
            Root.Add(simpleExpression);
        }

        public void AddCriteria(string property, string value, ExpressionOperator expressionOperator)
        {

            var simpleExpression = new SimpleExpression(property, value, expressionOperator);
            Root.Add(simpleExpression.ToXElement());

        }

        public void AddGreaterThanCriteria(string property, string value)
        {

            var simpleExpression = new SimpleExpression(property, value, ExpressionOperator.Greater);
            Root.Add(simpleExpression.ToXElement());

        }

        public void AddLessThanCriteria(string property, string value)
        {
            var simpleExpression = new SimpleExpression( property, value, ExpressionOperator.Less);
            Root.Add(simpleExpression.ToXElement());
        }

        public void AddEqualsCriteria(string property, string value)
        {
            var simpleExpression = new SimpleExpression(property, value, ExpressionOperator.Equal);
            Root.Add(simpleExpression.ToXElement());

        }
    }

    /*
    public class ProjectionCriteriaBuilder : CriteriaBuilder
    {

        //TODO What about different versions?
        protected ProjectionCriteriaBuilder(string nameSpace)
            : base(nameSpace)
        {
        }

        public ProjectionCriteriaBuilder()
            : base()
        {

        }



        public ObjectProjectionCriteria CreateCriteria(ManagementPackTypeProjection typeProjection, CriteriaElement element)
        {
            var root = GetRoot();
            Root.Add(element.ToXElement());
            return new ObjectProjectionCriteria(Root.ToString(), typeProjection, Context.CurrentManagementGroup);
        }

        public ObjectProjectionCriteria CreateSimpleCriteria(ManagementPackTypeProjection typeProjection, string property, string value)
        {
            var simpleExpression = new SimpleExpression(NameSpace, property, value, ExpressionOperator.Equal);
            Root.Add(simpleExpression.ToXElement());
            return new ObjectProjectionCriteria(Root.ToString(), typeProjection, Context.CurrentManagementGroup);
        }



    }
    */
    public class ObjectCriteriaBuilder : CriteriaBuilder
    {
        private readonly ManagementPackClass _class;

        public ObjectCriteriaBuilder(ManagementPackClass managementPackClass, Expression expression) : base(expression)      
        {
            
            _class = managementPackClass;
        }

        public EnterpriseManagementObjectCriteria GetCriteria()
        {
            return new EnterpriseManagementObjectCriteria(Root.ToString(), _class, Context.CurrentManagementGroup);
        }

    }
     
}
